//
//  YTTransitionAnimator.m
//  YiTuUserClient
//
//  Created by 石少庸 on 2017/2/27.
//  Copyright © 2017年 YiTu. All rights reserved.
//

#import "YTTransitionAnimator.h"

@implementation YTTransitionAnimator

// constants for transition animation
static const NSTimeInterval kForwardAnimationDuration         = 0.3;
static const NSTimeInterval kForwardCompleteAnimationDuration = 0.2;
static const NSTimeInterval kBackwardAnimationDuration         = 0.25;
static const NSTimeInterval kBackwardCompleteAnimationDuration = 0.18;

#pragma mark - <UIViewControllerAnimatedTransitioning>

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext {
    if (self.goingForward) {
        return kForwardAnimationDuration + kForwardCompleteAnimationDuration;
    } else {
        return kBackwardAnimationDuration + kBackwardCompleteAnimationDuration;
    }
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC   = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *containerView    = [transitionContext containerView];
    [containerView addSubview:fromVC.view];
    [containerView addSubview:toVC.view];
    
    Protocol *animating = @protocol(YTTransitionAnimating);
    BOOL doesNotConfirmProtocol = ![self.sourceTransition conformsToProtocol:animating] || ![self.destinationTransition conformsToProtocol:animating];
    if (doesNotConfirmProtocol) {
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        return;
    }
    
    UIView *alphaView = [[UIView alloc] initWithFrame:[transitionContext finalFrameForViewController:toVC]];
    alphaView.backgroundColor = [self.sourceTransition transitionSourceBackgroundColor];
    [containerView addSubview:alphaView];
    
    UIImageView *sourceImageView = [self.sourceTransition transitionSourceImageView];
    [containerView addSubview:sourceImageView];
    
    if (self.goingForward) {
        [UIView animateWithDuration:kForwardAnimationDuration delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            sourceImageView.frame = [self.destinationTransition transitionDestinationImageViewFrame];
            sourceImageView.transform = CGAffineTransformMakeScale(1.02, 1.02);
            alphaView.alpha = 0.9;
        } completion:^(BOOL finished) {
             [UIView animateWithDuration:kForwardCompleteAnimationDuration delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
                 alphaView.alpha = 0;
                 sourceImageView.transform = CGAffineTransformIdentity;
             } completion:^(BOOL finished) {
                 sourceImageView.alpha = 0;
                 if ([self.destinationTransition conformsToProtocol:@protocol(YTTransitionAnimating)] &&
                     [self.destinationTransition respondsToSelector:@selector(zoomTransitionAnimator:didCompleteTransition:animatingSourceImageView:)]) {
                     [self.destinationTransition zoomTransitionAnimator:self
                                                  didCompleteTransition:![transitionContext transitionWasCancelled]
                                               animatingSourceImageView:sourceImageView];
                 }
                 [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
                 [alphaView removeFromSuperview];
                 [sourceImageView removeFromSuperview];
             }];
        }];
        
    } else {
        [UIView animateWithDuration:kBackwardAnimationDuration delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            sourceImageView.frame = [self.destinationTransition transitionDestinationImageViewFrame];
            alphaView.alpha = 0;
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:kBackwardCompleteAnimationDuration delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
                sourceImageView.alpha = 0;
            } completion:^(BOOL finished) {
                if ([self.destinationTransition conformsToProtocol:@protocol(YTTransitionAnimating)] &&
                    [self.destinationTransition respondsToSelector:@selector(zoomTransitionAnimator:didCompleteTransition:animatingSourceImageView:)]) {
                    [self.destinationTransition zoomTransitionAnimator:self
                                                 didCompleteTransition:![transitionContext transitionWasCancelled]
                                              animatingSourceImageView:sourceImageView];
                }
                [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
                [alphaView removeFromSuperview];
                [sourceImageView removeFromSuperview];
            }];
        }];
    }
}

@end
